﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary;
using ObTookit.Model;

namespace ObTookit.Login
{
    public class UserDA
    {
        #region SELECT
        internal UserEO SelectByName(string userName)
        {
            return SelectByName(userName, 1);
        }
        internal UserEO SelectByName(string userName, int enabled)
        {
            UserEO user = new UserEO();
            string sql = "SELECT * FROM User WHERE Enabled = " + enabled + " AND UserName = '" + userName + "'";

            SQLiteCommand cmd = SQLiteHelper.CreateCommand(sql);
            DataSet ds = SQLiteHelper.ExecuteDataSet(cmd);
            DataTable dt = ds.Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                DataModelHandler<UserEO> dmh = new DataModelHandler<UserEO>();
                user = dmh.FillModel(dt.Rows[0]);
            }
            else
                user = null;

            return user;
        }

        internal List<UserEO> SelectList(int enabled)
        {
            List<UserEO> users = new List<UserEO>();
            string sql = "SELECT TOP 100 * FROM User WHERE Enabled = " + enabled;
            SQLiteCommand cmd = SQLiteHelper.CreateCommand(sql);
            DataSet ds = SQLiteHelper.ExecuteDataSet(cmd);
            DataTable dt = ds.Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                DataModelHandler<UserEO> dmh = new DataModelHandler<UserEO>();
                users = dmh.FillModel(dt);
            }
            return users;
        }
        #endregion
    }
}
